package com.per.mybootall.controller;

import com.github.pagehelper.PageInfo;
import com.per.mybootall.pojo.User;
import com.per.mybootall.service.HomeLoadInter;


import com.per.mybootall.service.impl.testNew;
import com.per.mybootall.vo.ProductCarVO;
import com.per.mybootall.vo.ProductVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@CrossOrigin(allowCredentials = "true",origins = "http://localhost:8080")
@Controller
@RequestMapping("load")
public class HomeLoadController {
    @Autowired
private HomeLoadInter homeLoadInter;
    @Autowired
    private testNew te;
    @Autowired
    private RedisTemplate redisTemplate;

    @CrossOrigin(origins = "http://localhost:8080",allowCredentials = "true")
    @RequestMapping(method = RequestMethod.POST,value = "/homeload")
    @ResponseBody
    public  List<ProductVO>   homeLoad(String callback){
        System.out.println("==>");
        //return list集合（图片，介绍信息，价格）
       List<ProductVO> list= homeLoadInter.selectProductMessage();
        return list;
    }



    @PostMapping(value = "/selectCondition")
    @ResponseBody
    public List<ProductVO> selectCondition(String selectCondition,String usertoken,String whoareuser){
        System.out.println("进入了条件查询的接口");
        System.out.println("条件查询的接口:"+selectCondition+","+usertoken+","+whoareuser);
        List<ProductVO> list= homeLoadInter.selectCondition(selectCondition, usertoken, whoareuser);
    if(null==list||list.size()==0){
        //身份验证不通过
        return null;
    }
    return list;
}


    @PostMapping(value = "/addCartBuy")
    @ResponseBody
    public String addCartBuy(String thisuserid,String productId,String usertoken,String whoareuser,int productNumber){
        System.out.println("加入购物车的接口:"+thisuserid+","+productId+","+usertoken+","+whoareuser+","+productNumber);
       String msg= homeLoadInter.addCartBuy(thisuserid,productId, usertoken, whoareuser,productNumber);
       return msg;
    }


    @PostMapping(value = "/showCartBuy")
    @ResponseBody
    public List<ProductCarVO> showCartBuy (String usertoken, String whoareuser, String thisuserid){
        System.out.println("展示购物车的接口:usertoken:"+usertoken+", whoareuser:"+whoareuser+",thisuserid:"+thisuserid);
         return homeLoadInter.showCarBuy(thisuserid,usertoken, whoareuser);
    }

    @GetMapping("/testRedisHash")
    public void testRedisHashh(){
        System.out.println("111");
        redisTemplate.opsForHash().put("cart","productid",666);
        System.out.println("222");
        redisTemplate.opsForHash().put("cart","product",888);

        Map<Object,Object> map=redisTemplate.opsForHash().entries("cart");
        for(Map.Entry<Object, Object> entry:map.entrySet()){
            System.out.println("key:"+entry.getKey()+";value:"+entry.getValue());
        }
//        System.out.println(aa);
    }

    /*购物车展示页面增加商品数量*/
    @PostMapping("/showCarAdd")
    @ResponseBody
    public String showCarAdd(String usertoken, String whoareuser,String thisuserid,String productId){
      return  homeLoadInter.whenShowProductNumberAdd(usertoken,  whoareuser, thisuserid, productId);
    }

    /*购物车展示页面减少商品数量*/
    @PostMapping("/showCardele")
    @ResponseBody
    public String showCardele(String usertoken, String whoareuser,String thisuserid,String productId){
        return  homeLoadInter.whenShowProductNumberLess(usertoken,  whoareuser, thisuserid, productId);
    }
}
